package com.chen.gulimall.gulimallproduct.service.impl;

import com.chen.gulimall.gulimallproduct.dao.AttrDao;
import com.chen.gulimall.gulimallproduct.entity.AttrAttrgroupRelationEntity;
import com.chen.gulimall.gulimallproduct.entity.AttrEntity;
import com.chen.gulimall.gulimallproduct.service.AttrService;
import com.chen.gulimall.gulimallproduct.vo.AttrGroupWithAttrVo;
import com.chen.gulimall.gulimallproduct.vo.AttrRelationVo;
import com.chen.gulimall.gulimallproduct.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.gulimallcommon.common.utils.PageUtils;
import com.chen.gulimallcommon.common.utils.Query;
import com.chen.gulimall.gulimallproduct.dao.AttrGroupDao;
import com.chen.gulimall.gulimallproduct.entity.AttrGroupEntity;
import com.chen.gulimall.gulimallproduct.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrGroupDao attrGroupDao;


    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    //在service层自定义处理代码（获取前端的params相关参数以及要查询的cailonId值）
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catlogId) {

        //当不输入指定的查询值时，默认将所有内容输出
        //page为分页查询工具类
        if (catlogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>());
                    return new PageUtils(page);
        }else {
            String key = (String) params.get("key");

            //queryWrapper为sql代码执行器，可以自己自定义代码
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catlogId);
            if (!StringUtils.isEmpty(key)) {
                queryWrapper.and(obj -> {
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper);
            return new PageUtils(page);

        }
    }

    @Override
    public void deleteRelation(AttrRelationVo[] vos) {
        List<AttrAttrgroupRelationEntity> entities = Arrays.asList(vos).stream().map((item) -> {
            AttrAttrgroupRelationEntity attrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item,attrgroupRelationEntity);
            return attrgroupRelationEntity;
        }).collect(Collectors.toList());

        attrGroupDao.deleteBatchRelation(entities);
    }

    //获取该categoryid下的对应分组所属的对应属性
    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttr(Long catelogId) {
        List<AttrGroupEntity> list = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));
        List<AttrGroupWithAttrVo> data = list.stream().map(item-> {
            AttrGroupWithAttrVo attrGroupWithAttrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(item,attrGroupWithAttrVo);
            //此方法为自己编写的方法，根据AttrGroupId来获取对应的组所属的属性，并且传入属性attrs中
            List<AttrEntity> attrs = attrService.getAttrRelation(item.getAttrGroupId());
            attrGroupWithAttrVo.setAttrs(attrs);
            return attrGroupWithAttrVo;
        }).collect(Collectors.toList());

        return data;
    }

    @Override
    public List<SpuItemAttrGroupVo> getGroupAttrForSpu(Long spuId, Long catalogId) {

       return baseMapper.getGroupAttrForSpu(spuId,catalogId);

    }

}